package interfaces.ltu;

//import com.droidnova.android.howto.optionmenu.R;

import java.util.Iterator;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class ScheduleActivity extends ListActivity {
	private Dialog editorDialog = null;

	/*	*//** Called when the activity is first created. */
	/*
	 * public void onCreate(Bundle savedInstanceState) {
	 * super.onCreate(savedInstanceState);
	 * setContentView(R.layout.schudelemain);
	 * 
	 * ImageButton next = (ImageButton) findViewById(R.id.ScheduleBackButton);
	 * 
	 * next.setOnClickListener(new View.OnClickListener() { public void
	 * onClick(View view) { Intent intent = new Intent(); setResult(RESULT_OK,
	 * intent); finish(); }
	 * 
	 * });
	 * 
	 * }
	 */

	// this is a hard coded list that we are using as the data
	// for the list in this example.
	private static String[] spirits = new String[] { "Vodka", "Gin", "Rum",
		"Tequila"
		/*
		 * ,"Scotch","Bourbon","Wiskey","Cognac", "Grapa","Cachaca","Rye",
		 * "Red Wine","White Wine", "Sake","Port"
		 */
	};

	// this is the controller that populates the list with data.
	public ArrayAdapter<String> listAdapter;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// reload any saved activity instance state
		super.onCreate(savedInstanceState);

		// set the activity to use the main.xml layout resource
		setContentView(R.layout.schudelemain);

		// gather the data to be used by the array
		collectListData();

		// set up the list adapter to be used by the ListView
		setupListAdapter();

	}

	/**
	 * This is where we create and connect the adapter to this activity as well
	 * as the data.
	 */
	private void setupListAdapter()
	{
		// "this" is the containing activity.
		// "android.R.layout.simple_list_item_1" is a predefined item that is
		//      already set up to work with a list adapter.  It is internal to
		//      the ListView with the id "@android:id/list".
		// "spirits" is the data array we are using in this example.
		//listAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, spirits );
		listAdapter = new ArrayAdapter<String>(this, R.layout.item,R.id.itemName);
		/*listAdapter.add("apple");
			AndroidInterfaceActivity.courseNos.add("apple");
			listAdapter.add("orange");
			AndroidInterfaceActivity.courseNos.add("orange");
			listAdapter.add("tomato");
			AndroidInterfaceActivity.courseNos.add("tomato");*/
		// connecting the list adapter to this ListActivity
		Iterator iterator;
		iterator = AndroidInterfaceActivity.courseLists.iterator();

		System.out.println("entry starts");
		while (iterator.hasNext()){
			String tmp=(String) iterator.next();
			System.out.println("tmp:"+tmp);
			if (tmp.length()>0)
				listAdapter.add(tmp);
		}
		String tmp="";
		int i =0;
			  
		setListAdapter(listAdapter);




	}

	/**
	 * This is where we would get data from a database or other location to plug
	 * into the list adapter. For this example the ListAdapter is an
	 * ArrayAdapter and the array is hard-coded. ListAdapter is an abstract
	 * class and there are many classes that extend it to provide connection
	 * with various data holding formats.
	 */
	private void collectListData() {
		// TODO: Gather data from a real source rather than hard-coding the
		// data.

	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.icon:
			/*Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG)
			.show();*/
			showDialog(0);
			break;
		case R.id.text:
			/*Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG)
			.show();*/
			listAdapter.remove(listAdapter.getItem(listAdapter.getCount() - 1));
			AndroidInterfaceActivity.courseLists.remove(listAdapter.getItem(listAdapter.getCount() - 1));
			System.out.println("Removed:"+ listAdapter.getItem(listAdapter.getCount() - 1));
			System.out.println("Size:"+ AndroidInterfaceActivity.courseLists.size());
			
			//ystem.out.println("Q Removed:"+ AndroidInterfaceActivity.courseLists.poll());
			System.out.println("QSize:"+ AndroidInterfaceActivity.courseLists.size());
			break;
			/*
			 * case R.id.icontext: Toast.makeText(this,
			 * "You pressed the icon and text!", Toast.LENGTH_LONG).show(); break;
			 */
		}
		return true;
	}

	protected Dialog onCreateDialog(int id) {
		Dialog editor = editorDialog;
		if (editorDialog == null) {
			editor = createEditorDialog();
		}
		return editor;
	}

	private Dialog createEditorDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(R.string.addDialogTitle);

		View content = getLayoutInflater().inflate(R.layout.editor,
				(ViewGroup) findViewById(R.id.editLayout));
		builder.setView(content);
		builder.setPositiveButton(R.string.addButtonLabel,
				new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				Dialog source = (Dialog) dialog;
				EditText nameField = (EditText) source
						.findViewById(R.id.itemField);
				String name = nameField.getText().toString();

				//EditText timesField = (EditText) source	.findViewById(R.id.timesField);
				//Integer times = Integer.valueOf(timesField.getText().toString());
				System.out.println(name);
				if (name.length() > 0) {
					listAdapter.add(name);
					AndroidInterfaceActivity.courseLists.add(name);
				}
				/*
				 * if ((name.length() > 0) && (times > 0)) {
				 * //listAdapter.add(name); for (int count = 0; count <
				 * times; count++) { //dataAdapter.add(name); } }
				 */
				dialog.dismiss();
			}
		});

		builder.setNegativeButton(R.string.cancelButtonLabel,
				new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});

		return builder.create();
	}
}
